<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
       <el-form-item label="房间类型" prop="typename">
        <el-popover
          ref="typeListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="typeList"
            :props="typeListTreeProps"
            node-key="id"
            ref="typeListTree"
            @current-change="typeListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.typename" v-popover:typeListPopover :readonly="true" placeholder="点击选择类别" class="menu-list__input"></el-input>
    </el-form-item>
    <el-form-item label="房间编号" prop="roomcode">
      <el-input v-model="dataForm.roomcode" placeholder="房间编号"></el-input>
    </el-form-item>
    <el-form-item label="房间名称" prop="roomname">
      <el-input v-model="dataForm.roomname" placeholder="房间名称"></el-input>
    </el-form-item>
    <el-form-item label="房间状态" prop="status">
      <el-radio-group v-model="dataForm.status">
        <el-radio :label="0">空房</el-radio>
        <el-radio :label="1">预定</el-radio>
        <el-radio :label="2">入住</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="楼层" prop="floor">
      <el-input v-model="dataForm.floor" placeholder="楼层"></el-input>
    </el-form-item>
    <el-form-item label="窗户情况" prop="iswindow">
    <el-radio-group v-model="dataForm.iswindow">
          <el-radio :label="0">有</el-radio>
          <el-radio :label="1">无</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="朝向" prop="towards">
      <el-input v-model="dataForm.towards" placeholder="朝向"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="subable" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
export default {
  data: function () {
    return {
      subable: false,
      visible: false,
      dataForm: {
        id: 0,
        typename: '',
        typeid: 0,
        roomcode: '',
        roomname: '',
        secretkey: '',
        floor: '',
        iswindow: 0,
        towards: '',
        area: '',
        sort: 0,
        status: 0
      },
      dataRule: {
        typename: [
          {required: true, message: '房间类型不能为空', trigger: 'blur'}
        ],
        roomcode: [
          {required: true, message: '房间编号不能为空', trigger: 'blur'}
        ],
        roomname: [
          {required: true, message: '房间名称不能为空', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '排序不能为空', trigger: 'blur'}
        ]
      },
      typeList: [],
      typeListTreeProps: {
        label: 'typename',
        children: 'children'
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/roomtype/roomtype/selectlist'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.typeList = treeDataTranslate(data.typeList, 'id')
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.subable = false
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (!this.dataForm.id) {
            // 新增
          this.typeListTreeSetCurrentNode()
        } else {
          // 修改
          this.$http({
            url: this.$http.adornUrl(`/roominfo/roominfo/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.typeid = data.roominfo.typeid
              this.dataForm.roomcode = data.roominfo.roomcode
              this.dataForm.roomname = data.roominfo.roomname
              this.dataForm.secretkey = data.roominfo.secretkey
              this.dataForm.floor = data.roominfo.floor
              this.dataForm.iswindow = data.roominfo.iswindow
              this.dataForm.towards = data.roominfo.towards
              this.dataForm.area = data.roominfo.area
              this.dataForm.sort = data.roominfo.sort
              this.dataForm.status = data.roominfo.status
              this.typeListTreeSetCurrentNode()
            }
          })
        }
      })
    },
     // 菜单树选中
    typeListTreeCurrentChangeHandle (data, node) {
      this.dataForm.typeid = data.id
      this.dataForm.typename = data.typename
    },
      // 菜单树设置当前选中节点
    typeListTreeSetCurrentNode () {
      this.$refs.typeListTree.setCurrentKey(this.dataForm.typeid)
      this.dataForm.typename = (this.$refs.typeListTree.getCurrentNode() || {})['typename']
    },
    // 表单提交
    dataFormSubmit () {
      this.subable = true
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/roominfo/roominfo/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'typeid': this.dataForm.typeid,
              'roomcode': this.dataForm.roomcode,
              'roomname': this.dataForm.roomname,
              'secretkey': this.dataForm.secretkey,
              'floor': this.dataForm.floor,
              'iswindow': this.dataForm.iswindow,
              'towards': this.dataForm.towards,
              'area': this.dataForm.area,
              'sort': this.dataForm.sort,
              'status': this.dataForm.status
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          this.subable = false
        }
      })
    }
  }
}
</script>
